/**
 * 自定义函数
 * */


var formList = {
    data: [],
    pages: {start: 0, limit: 10}
}; //数据列表


//列表
function getIndex(){
    var formData = {
        aid: getAid(),
        pageStart: formList.pages.start,
        pageLimit: formList.pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.shopIndex.method,
        url: domain + apis.shopIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                if (formList.pages.start > 0) {
                    msgPop({msg: res.msg});
                }
                return
            }
            for (var i = 0; i < res.data.length; i ++) {
                formList.data.push(res.data[i]);
            }
            formList.pages.start ++;
            getHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//列表字符串
function getHtml(){
    var h = '', i, d = formList.data;
    var id, shopCode, shopName, phone, head, lasttime;
    h += '';
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        shopCode = d[i].shop_code;
        shopName = d[i].shop_name;
        phone = d[i].phone==null ? '' : d[i].phone;
        head = d[i].head==null ? '' : '<img src="' + d[i].head + '" width="50"/>';
        lasttime = d[i].uptime==null ? '' : d[i].uptime;
        h += '<tr class="text-c">';
        h += '<td>' + id + '</td>';
        h += '<td>' + shopName + '</td>';
        h += '<td>' + shopCode + '</td>';
        if (head == '') {
            h += '<td>-</td>';
        } else {
            h += '<td>' + head + '</td>';
        }
        h += '<td>' + d[i].goods_num + '</td>';
        h += '<td>' + phone + '</td>';
        h += '<td>' + d[i].follow + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + lasttime + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" title="编辑" class="ml-5" onclick="viewEdit(' + id + ');">';
        h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a> &nbsp;';
        h += '<a href="' + routes.goods1Index + '?shopCode=' + shopCode + '&shopName=' + escape(shopName) + '" class="ml-5">';
        h += '商品<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    h += '';
    $('#formList').html(h);
    $('#tableMore').show();
}


////表单新增
//function viewAdd(){
//    var cw = document.documentElement.clientWidth;
//    var ch = document.documentElement.clientHeight;
//    var h = '';
//    h += '';
//    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
//    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
//    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
//    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">新增</div>';
//    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
//    //名称
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>门店名称：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<input type="text" class="input-text" placeholder="输入店名" id="shopName" value=""/>';
//    h += '</div>';
//    h += '</div>';
//    //描述
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2">门店描述：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<textarea class="textarea"  placeholder="说点什么...最少输入10个字符" datatype="*10-100" dragonfly="true" id="describe"></textarea>';
//    h += '</div>';
//    h += '</div>';
//    //头像
//    h += '<style type="text/css"></style>';
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2">门店头像：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">重新上传图片</div>';
//    h += '<input type="file" id="uploadFile" style="width: 180px; height: 30px; opacity: 0.1; cursor: pointer; position: relative; top: -30px; left: -180px;" onchange="uploadImage()"/>';
//    h += '<div id="img" style="margin-top: -20px; width: 100px; height: 100px; background: #eee; overflow: hidden;"></div>';
//    h += '</div>';
//    h += '</div>';
//    //老板
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>门店老板：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<input type="text" class="input-text" placeholder="输入老板手机号" id="phone" value=""/>';
//    h += '</div>';
//    h += '</div>';
//    //按钮
//    h += '<div class="row cl">';
//    //h += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">';
//    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
//    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="shopAdd();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
//    h += '</div>';
//    h += '</div>';
//    h += '</form>';
//    h += '</div>';
//    h += '</div>';
//    h += '';
//    $('#formPop').html(h);
//}


////显示图片
//function showImg(e){
//    var h = '';
//    h += '<img src="' + e + '" style="width: 150px;"/>';
//    h += '<input type="hidden" id="imgLink" value="' + e + '"/>';
//    //h += '<a href="javascript:;" style="padding: 5px 10px; color: #fff; background: red; font-size: 12px; border: 0; border-radius: 0; opacity: 0.7; position: relative; top: -110px; left: 0;" onclick="imgDel();">点击删除</a>';
//    $('#img').html(h);
//}


////新增门店
//function shopAdd(){
//    var shopName = $('#shopName').val();
//    var describe = $('#describe').val();
//    var img = $('#imgLink').val();
//    var phone = $('#phone').val();
//    if (shopName.length < 2) {
//        msgPop({msg: '门店名称不能少于2位'}); return
//    }
//    if (phone.length < 11) {
//        msgPop({msg: '老板手机号不能少于11位'}); return
//    }
//    if (img == undefined) {
//        img = '';
//    }
//    var formData = {
//        aid: getAid(),
//        shopName: shopName,
//        describe: describe,
//        img: img,
//        phone: phone
//    };
//    console.log(formData);
//    $.ajax({
//        type: apis.shopAdd.method,
//        url: domain + apis.shopAdd.url,
//        data: formData,
//        success: function (res) {
//            console.log(res);
//            if (res.code != 0) {
//                msgPop({msg: res.msg});
//                return
//            }
//            msgPop({msg: '添加成功'});
//            window.location.href = '';
//        },
//        error: function () {
//            console.log('网络错误');
//        }
//    });
//}


////表单修改
//function viewEdit(v){
//    var cw = document.documentElement.clientWidth;
//    var ch = document.documentElement.clientHeight;
//    var i, shopOne = '';
//    for (i = 0; i < formList.data.length; i ++) {
//        if (formList.data[i].id == v) {
//            shopOne = formList.data[i];
//        }
//    }
//    if (shopOne == '') {
//        msgPop({msg: '门店不存在'}); return
//    }
//    console.log(shopOne);
//    var h = '';
//    h += '';
//    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
//    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
//    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
//    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">修改</div>';
//    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
//    //名称
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>门店名称：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<input type="text" class="input-text" placeholder="输入店名" id="shopName" value="' + shopOne.shop_name + '"/>';
//    h += '<input type="hidden" id="shopCode" value="' + shopOne.shop_code + '"/>';
//    h += '</div>';
//    h += '</div>';
//    //描述
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2">门店描述：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<textarea class="textarea"  placeholder="说点什么...最少输入10个字符" datatype="*10-100" dragonfly="true" id="describe">' + shopOne.describe + '</textarea>';
//    h += '</div>';
//    h += '</div>';
//    //头像
//    h += '<style type="text/css"></style>';
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2">门店头像：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">重新上传图片</div>';
//    h += '<input type="file" id="uploadFile" style="width: 180px; height: 30px; opacity: 0.1; cursor: pointer; position: relative; top: -30px; left: -180px;" onchange="uploadImage()"/>';
//    h += '<div id="img" style="margin-top: -20px; width: 100px; height: 100px; background: #eee; overflow: hidden;">';
//    if (shopOne.head != '') {
//        h += '<img src="' + shopOne.head + '" style="width: 150px;"/>';
//    }
//    h += '<input type="hidden" id="imgLink" value="' + shopOne.head + '"/>';
//    h += '</div>';
//    h += '</div>';
//    h += '</div>';
//    //老板
//    h += '<div class="row cl">';
//    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>门店老板：</label>';
//    h += '<div class="formControls col-xs-8 col-sm-9">';
//    h += '<input type="text" class="input-text" placeholder="输入老板手机号" id="phone" value="' + shopOne.phone + '">';
//    h += '</div>';
//    h += '</div>';
//    //按钮
//    h += '<div class="row cl">';
//    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
//    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="shopUpdate();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
//    h += '</div>';
//    h += '</div>';
//    h += '</form>';
//    h += '</div>';
//    h += '</div>';
//    h += '';
//    $('#formPop').html(h);
//}


////更新门店
//function shopUpdate(){
//    var shopCode = $('#shopCode').val();
//    var shopName = $('#shopName').val();
//    var describe = $('#describe').val();
//    var img = $('#imgLink').val();
//    var phone = $('#phone').val();
//    if (shopCode == '' || shopCode == undefined) {
//        msgPop({msg: '门店代号错误'}); return
//    }
//    if (shopName.length < 2) {
//        msgPop({msg: '门店名称不能少于2位'}); return
//    }
//    if (phone.length < 11) {
//        msgPop({msg: '老板手机号不能少于11位'}); return
//    }
//    if (img == undefined) {
//        img = '';
//    }
//    var formData = {
//        aid: getAid(),
//        shopCode: shopCode,
//        shopName: shopName,
//        describe: describe,
//        img: img,
//        phone: phone
//    };
//    console.log(formData);
//    $.ajax({
//        type: apis.shopModify.method,
//        url: domain + apis.shopModify.url,
//        data: formData,
//        success: function (res) {
//            console.log(res);
//            if (res.code != 0) {
//                msgPop({msg: res.msg}); return
//            }
//            msgPop({msg: '修改成功'});
//            window.location.href = '';
//        },
//        error: function () {
//            console.log('网络错误');
//        }
//    });
//}